package com.app.hanoitips.travel;

import com.app.hanoitips.travel.welcome.WelcomView;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.preference.PreferenceManager;

public class MainActivity extends ActionBarActivity {
   MediaPlayer ourSound;
   WelcomView ourView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);// Removes title bar
        //Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //setContentView(R.layout.activity_main);
        //graphic welcome
        ourView=new WelcomView(this);
        setContentView(ourView);
        //sound welcome
        ourSound=MediaPlayer.create(MainActivity.this, R.raw.various_birds);
        SharedPreferences getPrefs=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        boolean music=getPrefs.getBoolean("checkbox", true);
        if(music==true){
        	ourSound.start();
        }
        
        Thread timer=new Thread(){
        	public void run(){
        		try {
					sleep(5000);
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				} finally{
					Intent openMainMenu=new Intent("android.intent.action.MainMenu");
					startActivity(openMainMenu);					
				}
        	}
        };
        timer.start();
        // exchange back to default image by savedInstanceState==null
        if (savedInstanceState != null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }
    @Override
    protected void onPause(){
    	super.onPause();
    	ourSound.release();
    	finish();
    }

      @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
      public static class PlaceholderFragment extends Fragment {

          public PlaceholderFragment() {
          }

          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState) {
              View rootView = inflater.inflate(R.layout.fragment_main, container, false);
              return rootView;
          }
      }

}
